package com.seanLab.tool.validator;

import com.seanLab.domain.EditorAddTag;
import com.seanLab.domain.EditorSession;
import com.seanLab.dto.FeedbackDto.*;
import com.seanLab.repository.EditorAddTagRepo;
import com.seanLab.repository.EditorSessionRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * Created by ailly on 17-10-7.
 */

@Service
public class FeedbackDtoValidator {
    @Autowired
    private EditorSessionRepo editorSessionRepo;

    public int validateFeedback(EditorFeedbackDto editorFeedbackDto) {

        int basicError = DtoValidator.validateBasicError(editorFeedbackDto);

        if (basicError == 0) {
            String vSessionID = editorFeedbackDto.getSessionID();
            List<String> vAdoptedImgs = editorFeedbackDto.getAdoptedImgs();

            if ( vSessionID != null && vSessionID.trim().length() != 0) {
                if (vAdoptedImgs != null && !vAdoptedImgs.isEmpty()) {
                    EditorSession editorSession = editorSessionRepo.findBySessionID(vSessionID);
                    if (editorSession != null) {
                        List<String> recommendedImgs = editorSession.getRecommendedImgs();
                        if (recommendedImgs.containsAll(vAdoptedImgs)) {
                            return 0;
                        } else {
                            return 21;
                        }
                    } else {
                        return 20;
                    }
                } else {
                    return 21;
                }
            } else {
                return 20;
            }

        } else {
            return basicError;
        }
    }

    public int validateAddTag(EditorAddTagDto editorAddTagDto) {
        int basicError = DtoValidator.validateBasicError(editorAddTagDto);

        if (basicError == 0) {
            String vSessionID = editorAddTagDto.getSessionID();
            String vImgID = editorAddTagDto.getImgID();
            List<String> vNewTags = editorAddTagDto.getNewTags();

            if ( vSessionID != null && vSessionID.trim().length() != 0) {
                if (vImgID != null && vImgID.trim().length() != 0) {
                    EditorSession editorSession = editorSessionRepo.findBySessionID(vSessionID);
                    if (editorSession != null) {
                        List<String> recommendedImgs = editorSession.getRecommendedImgs();
                        if (recommendedImgs.contains(vImgID)) {

                            return 0;
                        } else {
                            return 21;
                        }
                    } else {
                        return 20;
                    }
                } else {
                    return 21;
                }
            } else {
                return 20;
            }

        } else {
            return basicError;
        }
    }

    public int validateDeleteTag (EditorDeleteTagDto editorDeleteTagDto) {
        int basicError = DtoValidator.validateBasicError(editorDeleteTagDto);

        if (basicError == 0) {
            String vSessionID = editorDeleteTagDto.getSessionID();
            String vImgID = editorDeleteTagDto.getImgID();
            String vTagID = editorDeleteTagDto.getTagID();

            if ( vSessionID != null && vSessionID.trim().length() != 0) {
                if (vImgID != null && vImgID.trim().length() != 0) {
                    if (vTagID != null && vTagID.trim().length() != 0) {
                        EditorSession editorSession = editorSessionRepo.findBySessionID(vSessionID);
                        if (editorSession != null) {
                            List<String> recommendedImgs = editorSession.getRecommendedImgs();
                            if (recommendedImgs.contains(vImgID)) {
                                int i = recommendedImgs.indexOf(vImgID);
                                List<String> recommendTags = editorSession.getInnerImgTagIDs(i);
                                if (recommendTags.contains(vTagID)) {
                                    return 0;
                                } else {
                                    return 22;
                                }
                            } else {
                                return 21;
                            }
                        } else {
                            return 20;
                        }
                    } else {
                        return 22;
                    }
                } else {
                    return 21;
                }
            } else {
                return 20;
            }

        } else {
            return basicError;
        }
    }

    public int validateImageScore (EditorImageScoreDto editorImageScoreDto) {
        int basicError = DtoValidator.validateBasicError(editorImageScoreDto);

        if (basicError == 0) {
            String vSessionID = editorImageScoreDto.getSessionID();
           // String vImgID = editorAddTagDto.getImgID();

            List<EditorScoreSingleImgDto> vImgScoreList = editorImageScoreDto.getImgScoreList();


            if ( vSessionID != null && vSessionID.trim().length() != 0) {
                for (EditorScoreSingleImgDto e : vImgScoreList) {
                    String vImgID = e.getImgID();
                    int vScore = e.getScore();
                    String vReason = e.getReason();
                    String vNewTag = e.getNewTag();

                    if (vImgID != null && vImgID.trim().length() != 0) {
                        EditorSession editorSession = editorSessionRepo.findBySessionID(vSessionID);
                        if (editorSession != null) {
                            List<String> recommendedImgs = editorSession.getRecommendedImgs();
                            if (recommendedImgs.contains(vImgID)) {
                                //do something
                                if (vScore > 0 && vScore <= 5  ) {
                                    if (vScore <= 3) {
                                        if (vReason != null && vReason.trim().length() != 0) {
                                            //检测下一张图片
                                        } else {
                                            return 23;
                                        }
                                    } else {
                                        //检测下一张图片
                                    }
                                } else {
                                    return 23;
                                }
                            } else {
                                return 21;
                            }
                        } else {
                            return 20;
                        }
                    } else {
                        return 21;
                    }
                }

            } else {
                return 20;
            }

            return 0;

        } else {
            return basicError;
        }
    }
}